package com.sora.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sora.commonutils.JwtUtils;
import com.sora.commonutils.R;
import com.sora.eduservice.client.OrderClient;
import com.sora.eduservice.entity.EduCourse;
import com.sora.eduservice.entity.chapter.ChapterVo;
import com.sora.eduservice.entity.frontvo.CourseFrontVo;
import com.sora.eduservice.entity.frontvo.CourseWebVo;
import com.sora.eduservice.service.EduChapterService;
import com.sora.eduservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Classname: CourseFrontController
 * @Description:
 * @Author: Stonffe
 * @Date: 2023/1/13 15:04
 */
@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrderClient orderClient;
    @PostMapping("/getConditionPage/{page}/{limit}")
    public R getConditionPage(@PathVariable Long page,
                              @PathVariable Long limit,
                              @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> coursePage = new Page<>();
        Map<String,Object> map = courseService.getCourseFrontInfo(coursePage,courseFrontVo);
        return R.ok().data(map);
    }
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        boolean isBuyCourse = false;

        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        List<ChapterVo> chapterVoList = chapterService.getChapterVoByCourseId(courseId);
        //获取用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        if (!StringUtils.isEmpty(memberId)){
            //根据课程id、用户id，查询课程是否已经购买
            isBuyCourse = orderClient.isBuyCourse(memberId, courseId);
        }
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVoList).data("isBuy",isBuyCourse);
    }
}
